define(["dijit/form/MultiSelect", "dijit/form/Button", "dijit/layout/ContentPane", "dojo/dom", "dojo/json", "dojo/request", "dojo/_base/window",
	"dojo/_base/declare", "dojo/parser", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/registry", "dojo/ready"], function(MultiSelect, Button, 
		ContentPane, dom, JSON, request, win, declare, parser, _WidgetBase, _TemplatedMixin, registry, ready){
		
		return declare("ListTables", [_WidgetBase, _TemplatedMixin], {
			templateString:
                "<div>" +
				"<div class='listtables'>" +
					"<button data-dojo-attach-event='onclick: createTable'>Create Table</button>" +
					"<button data-dojo-attach-event='onclick: removeTables'>Remove Table</button>" +
					"<select data-dojo-type='dijit/form/MultiSelect' id='tables' name='tables'></select>" +
				"</div>" +
                "</div>",
				
			createTable: function(){
				window.location = "/importIssues"
			},
			
			removeTables: function(){
				registry.byId("tables").getSelected().foreach(function(n){
					request.post("tables", {
						data: JSON.stringify({opType: "remove", value:n.value}),
						handleAs: "json"}).then(function(data){
							data = JSON.parse(data);
							if (data == "Successful") {
								tables.destory(n);
							}
						});
					);
				}
			}
		});
		
		ready(function() {
		
			parser.parse();
			request.post("tables", {
				data: JSON.stringify({opType: "database"}),
				handleAs: "json"}).then(function(data){
					var attr = dom.byId('tables');
					for (var i = 0; i < data.length; i++) {
						record = data[i];
						var add = win.doc.createElement('option');
						add.innerHTML = record;
						add.value = record;
						attr.appendChild(add);
					}
				});
		});
		
	});
